package tools;

import constants.AboutDbg;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.FileOutputStream;

public class FileoutputUtil {

    // Logging output file
    public static final String Acc_Stuck = "Logs/Log_AccountStuck.txt",
	    Login_Error = "Logs/Log_Login_Error.txt",
	    Timer_Log = "Logs/Log_Timer_Except.txt",
	    MapTimer_Log = "Logs/Log_MapTimer_Except.txt",
	    GMCommand_Log = "Logs/Log_GMCommand.txt",
	    IP_Log = "Logs/Log_AccountIP.txt",
	    Horntail_Log = "Logs/Log_Horntail.txt",
	    Pinkbean_Log = "Logs/Log_Pinkbean.txt",
            Zakum_Log = "Logs/Log_Zakum.txt",
            Cygnus_Log = "Logs/Log_Cygnus.txt";
    // End
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void log(final String file, final String msg) {
	FileOutputStream out = null;
	try {
	    out = new FileOutputStream(file, true);
	    out.write(msg.getBytes());
	    out.write((System.getProperty("line.separator")+"------------------------"+System.getProperty("line.separator")).getBytes());
	} catch (IOException ess) {
	} finally {
	    try {
		if (out != null) {
		    out.close();
		}
	    } catch (IOException ignore) {
	    }
	}
    }

    public static void outputFileError(final String file, final Throwable t) {
	FileOutputStream out = null;
	try {
	    out = new FileOutputStream(file, true);
	    out.write(getString(t).getBytes());
	    out.write("\n------------------------\n".getBytes());
	} catch (IOException ess) {
	} finally {
	    try {
		if (out != null) {
		    out.close();
		}
	    } catch (IOException ignore) {
	    }
	}
    }

    public static final String CurrentReadable_Time() {
	return sdf.format(Calendar.getInstance().getTime());
    }

    public static final String getString(final Throwable e) {
	String retValue = null;
	StringWriter sw = null;
	PrintWriter pw = null;
	try {
	    sw = new StringWriter();
	    pw = new PrintWriter(sw);
	    if (!AboutDbg.realese) e.printStackTrace(pw);
	    retValue = sw.toString();
	} finally {
	    try {
		if (pw != null) {
		    pw.close();
		}
		if (sw != null) {
		    sw.close();
		}
	    } catch (IOException ignore) {
	    }
	}
	return retValue;
    }
}
